package com.study.config;

import com.zaxxer.hikari.HikariDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;
import java.util.HashMap;

/**
 * @author : zhuyanpeng.zhuyanpeng
 * @Description: :
 * @date :Create in  2019/9/10- 14:39
 * @Version: V1.0
 * @Modified By:
 **/
@Configuration
public class DataSourceConfig {

    @Bean("test1DataSource")
    public DataSource test1DataSource(DBConfig1 dbConfig1){
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setUsername(dbConfig1.getUsername());
        dataSource.setJdbcUrl(dbConfig1.getUrl());
        dataSource.setPassword(dbConfig1.getPassword());
        dataSource.setDriverClassName(dbConfig1.getDriverClassName());
        dataSource.setMaximumPoolSize(dbConfig1.getMinPoolSize());
        dataSource.setMinimumIdle(dbConfig1.getMaxPoolSize());
        return dataSource;
    }


    @Bean("test2DataSource")
    public DataSource test2DataSource(DBConfig2 dbConfig2){
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setUsername(dbConfig2.getUsername());
        dataSource.setJdbcUrl(dbConfig2.getUrl());
        dataSource.setPassword(dbConfig2.getPassword());
        dataSource.setDriverClassName(dbConfig2.getDriverClassName());
        dataSource.setMaximumPoolSize(dbConfig2.getMinPoolSize());
        dataSource.setMinimumIdle(dbConfig2.getMaxPoolSize());
        return dataSource;
    }


    @Bean("dataSource")
    @Primary
    public DataSource dataSource(DataSource test1DataSource,DataSource test2DataSource){
        HashMap<String, DataSource> result = new HashMap<>(2);
        result.put(DataSourceEnums.FIRST,test1DataSource);
        result.put(DataSourceEnums.SECOND,test2DataSource);
        return new DynamicRoutingDataSource(test1DataSource,result);
    }


    @Bean("sqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory(@Value("${mybatis.mapperLocations}")  String mapperLocations,
                                               @Value("${mybatis.type-aliases-package}")  String typeAliasesPackage,
                                               @Value("${mybatis.config-location}")  String configLocation,
                                               @Qualifier("dataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        //Mapper.xml 映射路径
        sqlSessionFactoryBean.setMapperLocations( new PathMatchingResourcePatternResolver()
                .getResources(mapperLocations));
        //package 映射路径
        sqlSessionFactoryBean.setTypeAliasesPackage(typeAliasesPackage);
        //mybatis Config 配置
        ClassPathResource classPathResource = new ClassPathResource(configLocation);
        sqlSessionFactoryBean.setConfigLocation(classPathResource);
        return sqlSessionFactoryBean.getObject();
    }

    @Bean
    PlatformTransactionManager transactionManager(@Qualifier("dataSource") DataSource dataSource){
        return  new DataSourceTransactionManager(dataSource);
    }

}
